<?php namespace App\Admin\Cabinet\Cabinet\Listeners;

use App\Core\Exceptions\RuntimeError;
use App\Core\Service\Listeners\CreateListener as Base;

use App\Admin\Cabinet\CabinetCell\Repository as CellRepository;

class CreateListener extends Base {
    
    public function onCreateSaved(){
        $cabinetId = $this->service->createModel->id;
        $type = $this->service->createModel->type;
        if(!$type){
            throw new RuntimeError('型号不存在');
        }
        $items = [];
        $rows = intval($type->rows);
        $cols = intval($type->cols);
        for($i=1;$i<=$rows;$i++){
            for($j=1;$j<=$cols;$j++){
                $items[] = [
                    'cabinet_id' => $cabinetId,
                    'row' => $i,
                    'col' => $j
                ];
            }
        }
        $rep = new CellRepository();
        foreach($items as $item){
            $rep->create($item);
        }
    }
}